// pages/user/info.js
import http from '../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loaded:false,
    name:'',
    sex:1,
    birthday:'',
    birthdayTxt:'保存后不可修改',
    emotion:'',
    emotionList:['单身','恋爱中','已婚','已育'],
    mouthfeel:''
  },
  goKgset(){
    wx.navigateTo({
      url: 'kgset/kgset',
    })
  },
  bindName(e){
    this.setData({
      name:e.detail.value
    });
  },
  saveinfo(){
    let me = this;
    if (!me.data.name){
      wx.showToast({
        icon:'none',
        title: '姓名必填'
      })
      return;
    }
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'user/saveInfo',
      loadTip:'保存信息中',
      data:{
        userId:userId,
        sex: me.data.sex,
        birthday: me.data.birthday,
        emotion: me.data.emotion,
        mouthfeel: me.data.mouthfeel,
        name: me.data.name
      },
      success: res => {
        wx.showToast({
          title: '修改成功'
        })
      }
    });
  },
  getUserInfo(type){
    let me = this;
    let userId = wx.getStorageSync("userId");
    if(!userId){
      return;
    }
    http.request({
      url:'user/getUserInfo/'+userId,
      success:res=>{
        if(type==1){
          me.setData({
            sex: res.sex,
            birthday: res.birthday,
            birthdayTxt: res.birthday,
            emotion: res.emotion,
            mouthfeel: res.mouthfeel,
            name: res.name
          });
        }else{
          let choseKgs = wx.getStorageSync("choseKgs");
          if (choseKgs) {
            let kgs = choseKgs.toString();
            setTimeout(() => {
              me.setData({
                mouthfeel: kgs
              });
              wx.removeStorageSync("choseKgs");
            }, 120)
          }
        }
      }
    });
  },

  bindMouthfeelChange(e){
    this.setData({
      mouthfeel: this.data.mouthfeelList[e.detail.value]
    });
  },
  bindQgChange(e){
    this.setData({
      emotion: this.data.emotionList[e.detail.value]
    });
  },
  sexChange(e){
    this.setData({
      sex:e.detail.value
    });
  },
  bindBirthdayChange(e){
    this.setData({
      birthday: e.detail.value,
      birthdayTxt:e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '个人信息',
    })
    this.getUserInfo(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo(2);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})